package br.gendi.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Configuracoes {

	private static boolean ativo;
	private static String host;
	private static String port;
	private static String user;
	private static String password;
	
	public static void imprime() {
		System.out.println(ativo);
		System.out.println(host);
		System.out.println(port);
		System.out.println(user);
		System.out.println(password);
	}
	
	public static boolean isProxyAtivo() {
		return ativo;
	}
	public static String getHost() {
		return host;
	}
	public static String getUser() {
		return user;
	}
	public static String getPassword() {
		return password;
	}
	public static String getPort() {
		return port;
	}
	private static void setUser(String user) {
		Configuracoes.user = user;
	}
	private static void setPort(String port) {
		Configuracoes.port = port;
	}
	private static void setPassword(String password) {
		Configuracoes.password = password;
	}
	private static void setHost(String host) {
		Configuracoes.host = host;
	}
	private static void setAtivo(boolean ativo) {
		Configuracoes.ativo = ativo;
	}
		
	public static Properties loadProxy() {
		Properties prop = new Properties();
	    FileInputStream fis = null;
		try {
			fis = new FileInputStream("proxy.xml");
		} catch (FileNotFoundException e) {
			try {
				new File("proxy.xml").createNewFile();
			} catch (IOException e1) {	
				e.printStackTrace();
			}
		}
	    try {
			prop.loadFromXML(fis);
		} catch (IOException e) {	
			//e.printStackTrace();
		}
		
		if (prop.getProperty("ativo") != null) {
			if (prop.getProperty("ativo").toString().equals(true)) {
				ativo = true;
			} else {
				ativo = false;
			}		
			host = prop.getProperty("host");
			port = prop.getProperty("port");
			user = prop.getProperty("user");
			password = prop.getProperty("password");
			
		}
		
	    return prop;
	}

	public static void salvarProxy(boolean ativo, String host, String port, String user, String pass) {
		
		setAtivo( ativo );
		setHost( host );
		setPort( port );
		setUser( user );
		setPassword( pass );
		
		Properties prop = new Properties();
	    prop.setProperty("ativo", ativo?"true":"false");
	    prop.setProperty("host", host);
	    prop.setProperty("port", port);
	    prop.setProperty("user", user);
	    prop.setProperty("password", pass);
	    FileOutputStream fos = null;
		try {
			fos = new FileOutputStream("proxy.xml");
		} catch (FileNotFoundException e) {
			try {
				new File("proxy.xml").createNewFile();
			} catch (IOException e1) {	}
		}
	    try {
			prop.storeToXML(fos, "Proxy Configuration");
		} catch (IOException e) {	}
	    try {
			fos.close();
		} catch (IOException e) {	}
	}

	public static void setProxy() {
		Properties p = Configuracoes.loadProxy();
		
		if (p.getProperty("ativo").equals("true")) {
			setAtivo(true);
		} else {
			setAtivo(false);
		}
		
		setHost(p.getProperty("host"));
		setPort(p.getProperty("port"));
		setUser(p.getProperty("user"));
		setPassword(p.getProperty("password"));
	
	}
}
